<cfscript>
	param name="FORM.FirstName" default="";
	param name="FORM.LastName" default="";

	param name="FORM.UserName" default="";
	param name="FORM.Password1" default="";
	param name="FORM.Password2" default="";
	param name="FORM.Email" default="";

	ERRORS = StructNew();
	if(StructKeyExists(FORM, "action")){
		login = EntityNew('MySCF');

		try { login.setFirstName(FORM.FirstName); }
		catch(any e){ ERRORS.FirstName = e.message; }

		try { login.setLastName(FORM.LastName); }
		catch(any e){ ERRORS.LastName = e.message; }

		try { login.setEmail(FORM.Email); }
		catch(any e){ ERRORS.Email = e.message; }

		try { login.setUsername(FORM.Username); }
		catch(any e){ ERRORS.Username = e.message; }

		if(Compare(FORM.Password1, FORM.Password2) NEQ 0){
			ERRORS.Password = "Passwords do not match";
		} else {
			try { login.setPassword(FORM.Password1); }
			catch(any e){ ERRORS.Password = e.message; }
		}

		if(StructIsEmpty(ERRORS)){
			EntitySave(login);
			ORMFlush();

			code = Hash(login.getSalt(), 'MD5');
			include "activation_email.cfm";

			message = new mail();
			message.setTo(FORM.Email);
			message.setFrom("MySCF <noreply@scf.cc>");
			message.setSubject("MySCF: Activation");
			message.setType("html");
			message.setBody(emailbody);
			message.send();

			Location("activate.cfm?username=" & login.getUsername(), false, 302);
		}
	}
</cfscript>
<html>
	<head>
		<title>MySCF Signup</title>
		<style type="text/css">
			* { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; }
			td { font-size: 14px; }
			td.e { color: red; }
			#un_status { width: 300px; height:50px; color: #f00; }
			#un_indi { display: none; }
		</style>
		<script type="text/javascript" src="/jquery/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">
			var un_dirty = 0;

			setTimeout("checkusername()", 500);
			function checkusername()
			{
				if(un_dirty != 0 && (new Date().getTime() - un_dirty) > 500){
					$.ajax({
						aync: false,
						url: 'validate.cfm',
						dataType: 'json',
						type: 'POST',
						data: { username: $("input[name='UserName']").val() },
						success: function(json,status,xhr)
						{
							if(json.username == $("input[name='UserName']").val()){
								$('#un_indi').hide();
								var status = $('#un_status');
								if(json.valid){ status.css('color', '#0f0'); }
								else { status.css('color', '#f00'); }

								status.html('* ' + json.message);
								un_dirty = 0;
							}
						}
					});
				}
				setTimeout("checkusername()", 500);
			}

			function updateusername()
			{
				un_dirty = new Date().getTime();
				$('#un_status').html('');
				$('#un_indi').show();
			}

			$(function(){ $("input[name='FirstName']").focus(); });
		</script>
	</head>
	<body>
		<form method="post" action="index.cfm">
			<cfoutput>
			<input type="hidden" name="action" value="signup" />
			<table>
				<tr>
					<td>First Name:</td>
					<td>
						<input type="text" name="FirstName" size="30"
							value="#HTMLEditFormat(FORM.FirstName)#" />
					</td>
					<cfif StructKeyExists(ERRORS, "FirstName")>
					<td class="e">* #ERRORS.FirstName#</td>
					</cfif>
				</tr>
				<tr>
					<td>Last Name:</td>
					<td>
						<input type="text" name="LastName" size="30"
							value="#HTMLEditFormat(FORM.LastName)#" />
					</td>
					<cfif StructKeyExists(ERRORS, "LastName")>
					<td class="e">* #ERRORS.LastName#</td>
					</cfif>
				</tr>
				<tr>
					<td>Email Address:</td>
					<td>
						<input tyep="text" name="Email" size="30"
							value="#HTMLEditFormat(FORM.Email)#" />
					</td>
					<cfif StructKeyExists(ERRORS, "Email")>
					<td class="e">* #ERRORS.Email#</td>
					</cfif>
				</tr>
			</table>

			<br/>

			<table>
				<tr>
					<td>MySCF username:</td>
					<td>
						<input type="text" name="UserName" size="12"
							onpropertychange="updateusername()"
							oninput="updateusername()"
							value="#HTMLEditFormat(FORM.UserName)#"/>
					</td>
					<td id="un_indi"><img src="../images/bar.gif" /></td>
					<td id="un_status">
						<cfif StructKeyExists(ERRORS, "UserName")>
						* #ERRORS.UserName#
						</cfif>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table>

			<br/>

			<table>
				<tr>
					<td>Enter your password:</td>
					<td>
						<input type="password" name="Password1" size="25" />
					</td>
					<cfif StructKeyExists(ERRORS, "Password")>
					<td class="e" rowspan="2" style="vertical-align: center">
						* #ERRORS.Password#
					<td>
					</cfif>
				</tr>
				<tr>
					<td>Re-enter your password:</td>
					<td>
						<input type="password" name="Password2" size="25" />
					</td>
				</tr>
			</table>

			<br/><br/>

			<input type="submit" name="submitbtn" value="Create Account" />
			</cfoutput>
		</form>
	</body>
</html>